﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.Year;

namespace Benefit.Service.Year
{
    public class ServiceCompanyYearSum : Benefit.Interface.Year.ICompanyYearSum
    {
        Benefit.DB.DBManager db = null;
        public ServiceCompanyYearSum(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        /// <summary>
        /// 保存月总值
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitCompanyYearSum(int year)
        {
            CompanyYearSum sum = new CompanyYearSum();
            var query = db.CompanyYearSum.Where(a => a.Year == year);
            var queryMonthList = db.CompanyMonthSum.Where(a => a.Year == year);
            if (query.Count() != 0)
            {
                sum = query.First();
            }
            sum.AvgBillBenefit = queryMonthList.Sum(a => a.AvgBillBenefit);
            sum.AvgPostionTime = queryMonthList.Sum(a => a.AvgPostionTime);
            sum.BillCount = queryMonthList.Sum(a => a.BillCount);
            sum.DayCount = queryMonthList.Sum(a => a.DayCount);
            sum.Free = queryMonthList.Sum(a => a.Free);
            sum.LoseCount = queryMonthList.Sum(a => a.LoseCount);
            sum.OverStopLoss = queryMonthList.Sum(a => a.OverStopLoss);
            sum.MaxReturn = queryMonthList.Sum(a => a.MaxReturn);
            sum.PingJinPing = queryMonthList.Sum(a => a.PingJinPing);
            sum.Profit = queryMonthList.Sum(a => a.Profit);
            sum.RaiseRate = queryMonthList.Sum(a => a.RaiseRate);
            sum.RiskCount = queryMonthList.Sum(a => a.RiskCount);
            sum.SumLost = queryMonthList.Sum(a => a.SumLost);
            sum.SumWin = queryMonthList.Sum(a => a.SumWin);
            sum.UsedMargin = queryMonthList.Sum(a => a.UsedMargin);
            sum.Utilization = queryMonthList.Sum(a => a.Utilization);
            sum.WinCount = queryMonthList.Sum(a => a.WinCount);
            sum.WinRate = queryMonthList.Sum(a => a.WinRate);
            sum.Yield = queryMonthList.Sum(a => a.Yield);
            sum.Year = year;
            if (query.Count() == 0)
            {
                db.CompanyYearSum.Add(sum);
            }

            db.SaveChanges();
        }



        /// <summary>
        /// 获取公司日总值
        /// </summary>
        /// <param name="accountid"></param>
        /// <param name="tradeHistoryId"></param>
        /// <returns></returns>
        public CompanyYearSum GetCompanyYearSum(int year)
        {
            CompanyYearSum count = new CompanyYearSum();
            var query = db.CompanyYearSum.Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                count = query.First();
            }
            return count;
        }

        public void Delete(int year)
        {
            var query = db.CompanyYearSum.Where(a => a.Year == year);
            if (query.Count() > 0)
            {
                db.CompanyYearSum.Remove(query.First());
                db.SaveChanges();
            }
        }
    }
}
